﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCTAccess.Business;
using System.Data;
using DCTAccess.Business.Member;
using DCTLib.Common;

namespace DCTAccess.Business.Member
{
    public class MemberController
    {
        #region [Control Action] ADD_NEW  /   UPDATE  /   DELETE  /   CHANGE_PASS /   UPDATE_LAST_LOGIN
        public static bool Insert(Member member)
        {
            return ExecuteStoredProcedure(member, StoreProceduAction.AddNew.ToString());
        }

        public static bool Update(Member member)
        {
            return ExecuteStoredProcedure(member, StoreProceduAction.Update.ToString());
        }

        public static bool Delete(Member member)
        {
            return ExecuteStoredProcedure(member, StoreProceduAction.Delete.ToString());
        }

        public static bool ChangePassword(Member member)
        {
            return ExecuteStoredProcedure(member, "ChangePassword");
        }

        public static bool UpdateLastLoginDate(Member member)
        {
            return ExecuteStoredProcedure(member, "UpdateLastLoginDate");
        }
        #endregion

        public static MemberCollection GetMember(MemberFilter searchCriteria)
        {
            DataTable memberDatatable = GetMembersByFilter(searchCriteria);

            if (ValidationHelper.HasRow(memberDatatable))
            {
                return new MemberCollection(memberDatatable);
            }
            else
            {
                return null;
            }
        }

        public static DataTable GetProductByUserID(int id)
        {           
            SQLHelper sqlHelper = new SQLHelper();
            string sProc = "Product_GetByCreateBy";
            sqlHelper.AddParameter("@CreateBy", id);
            return sqlHelper.GetDataTable(sProc);
        }

        public static Member GetMemberByID(int memberID)
        {
            MemberFilter searchCriteria = new MemberFilter();
            searchCriteria.ID = memberID;

            DataTable dtMember = searchCriteria.GetMember();
            if (ValidationHelper.HasRow(dtMember))
            {
                return new Member(dtMember.Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public static Member GetMemberByUniqueCode(string unique)
        {
            MemberFilter filter = new MemberFilter();
            filter.UniqueCode = unique;

            DataTable dt = filter.GetMember();
            if (ValidationHelper.HasRow(dt))
            {
                return new Member(dt.Rows[0]);
            }
            else
            {
                return null;
            }            
        }


        public static Member GetMemberByUsername(string username)
        {
            MemberFilter memberFilter = new MemberFilter();
            memberFilter.UserName = username;

            DataTable dtMember = memberFilter.GetMember();
            if (ValidationHelper.HasRow(dtMember))
            {
                return new Member(dtMember.Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public static DataTable GetMembersByFilter(MemberFilter searchCriteria)
        {
            return searchCriteria.GetMember();
        }

        private static bool ExecuteStoredProcedure(Member member, string action)
        {
            SQLHelper sqlHelper = new SQLHelper();
            string sProc = "Members_spr";
            sqlHelper.AddParameter("@Action", action);
            sqlHelper.AddParameter("@MemberID", member.ID);
            sqlHelper.AddParameter("@Username", member.UserName);
            sqlHelper.AddParameter("@HashedPassword", member.HashedPassword);
            sqlHelper.AddParameter("@FullName", member.FullName);            
            sqlHelper.AddParameter("@Mobile", member.Mobile);
            sqlHelper.AddParameter("@Address", member.Address);
            sqlHelper.AddParameter("@Email", member.Email);
            sqlHelper.AddParameter("@UniqueCode", member.UniqueCode);
            sqlHelper.AddParameter("@LevelInfo", member.LevelInfo);
            return sqlHelper.ExecuteSQL(sProc);
        }

        public static bool ChangePassMember(Member member)
        {            
            SQLHelper sqlHelper = new SQLHelper();
            string sProc = "ChangePass_Members";
            sqlHelper.AddParameter("@MemberID", member.ID);
            sqlHelper.AddParameter("@HashedPassword", member.HashedPassword);
            return sqlHelper.ExecuteSQL(sProc);
        }
    }
}
